# utf-8
import random

import datetime
from flask_restful import Resource, fields, marshal_with, reqparse

# 处理天气页面的诗歌请求
from common.mongodb import MongoDB


class Poem(Resource):
    content_fields = {
        'title': fields.String,
        'author': fields.String,
        'content': fields.String
    }

    selected_fields = {
        'key': fields.String,
        'c': fields.List(fields.String)
    }

    @marshal_with(selected_fields)
    def get(self):
        db = MongoDB("poem")
        if datetime.datetime.now().month in range(3,6):
            verse_list = list(db.find_many("verse", season="春天"))
        elif datetime.datetime.now().month in range(6,9):
            verse_list = list(db.find_many("verse", season="夏天"))
        elif datetime.datetime.now().month in range(9,12):
            verse_list = list(db.find_many("verse", season="秋天"))
        else:
            verse_list = list(db.find_many("verse", season="冬天"))
        verse_key = random.choice(verse_list)
        data = dict(
            key=verse_key["key"],
            c=verse_key['c']
        )
        return data, 200

    @marshal_with(content_fields)
    def post(self):
        db = MongoDB("poem")
        parser = reqparse.RequestParser()
        parser.add_argument('key', type=str)
        args = parser.parse_args()
        key = args['key']
        poem_content = db.find_one("content", key=key)
        data = dict(title=poem_content['title'],
                    author=poem_content['author'],
                    content=poem_content['content'])
        return data, 200
